// State operations

function Para_State() {
    // methods
    this.mark_message_as_read      = Para_State_MarkMessageAsRead;
    this.init_read_msg_array       = Para_State_InitReadMsgArray;
    this.is_message_read           = Para_State_IsMessageRead;
    this.mark_message_as_read      = Para_State_MarkMessageAsRead;
    this.mark_message_as_unread    = Para_State_MarkMessageAsUnread;    
    this.save_read_messages        = Para_State_SaveReadMessages;
    this.clean_up_in_read_messages = Para_State_CleanUpInReadMessages;

    this.set_token                 = Para_State_SetToken;
    this.get_token                 = Para_State_GetToken
    this.set_screen_name           = Para_State_SetScreenName;
    this.get_screen_name           = Para_State_GetScreenName

    // properties
    // Use two cookies, because of the 4K size limit
    this.hash            = new Hash.Cookie('ParaSite_Web',  { duration: 365 });
    this.tokenhash       = new Hash.Cookie('ParaSite_Auth', { duration: 365 })

    this.token           = this.tokenhash.get('token');
    this.screen_name     = this.tokenhash.get('screen_name');

    this.read_messages   = null;

    this.init_read_msg_array(); 

}


function Para_State_InitReadMsgArray() {

    var s = this.hash.get('read');

    if(s == null) {
        this.read_messages = new Array();
    }
    else {
        var old   = s.split(':');
        var fresh = new Array();
        for(var i = 0; i < old.length; i++)
            if( (old[i] != null) && (old[i] != '') )
                fresh.push( old[i] );

        this.read_messages = fresh;
    }
}


function Para_State_MarkMessageAsRead( id ) {
    if( this.is_message_read( id ) )
        return;
    this.read_messages.push( id );
    this.save_read_messages();
}


function Para_State_MarkMessageAsUnread( id ) {
    for(var i = 0; i < this.read_messages.length; i++)
        if(id == this.read_messages[i])
            this.read_messages[i] = 0;
    this.save_read_messages();
}


function Para_State_IsMessageRead( id ) {
    if(this.read_messages == null)
        return false;

    for(var i = 0; i<this.read_messages.length; i++)
        if(id == this.read_messages[i])
            return true;
    
    return false;
}


// remove non-active IDs from the read message list
function Para_State_CleanUpInReadMessages( active_ids ) {
    var fresh = new Array();
    for( var i = 0; i < active_ids.length; i++ )
        if( this.is_message_read( active_ids[i] ) )
           fresh.push( active_ids[i] );

    this.read_messages = fresh;
    this.save_read_messages();
}


function Para_State_SaveReadMessages() {
    if(this.read_messages == null)
        return;

    var s = this.read_messages.join(':');
    //para_debug("Read messages :" + s );

    this.hash.set( 'read', s );
    this.hash.save();
}

function Para_State_SetToken( token ) {
    this.tokenhash.set('token', token);
    this.tokenhash.save();
}


function Para_State_GetToken() {
    return this.tokenhash.get('token');

}


function Para_State_SetScreenName( sn ) {
    this.tokenhash.set('screen_name', sn);
    this.tokenhash.save();
}


function Para_State_GetScreenName() {
    return this.tokenhash.get('screen_name');

}
